package com.renyi.model;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;



/**
 * 基于Mybatis的数据库逻辑处理实现类 MybatisServiceImpl.java Create on 2014-4-29
 * @author dasuan
 */
@Service
public class MybatisServiceDoctorImpl implements MybatisServiceDoctor{
	@Autowired
	private MybatisRepositoryDoctor mybatisRepositoryTrack;
	protected final Log logger = LogFactory.getLog(MybatisService.class);

	public MybatisServiceDoctorImpl() {
	};

	public MybatisServiceDoctorImpl(MybatisRepositoryDoctor mybatisRepository) {
		this.mybatisRepositoryTrack = mybatisRepository;
	}

	/**
	 * 查询记录
	 * @author dasuan
	 * @version 1.0
	 * @param statement ,执行的语句名称
	 * @return List<?>
	 */
	public List<HashMap<String, String>> findMybatisList(String statement) {
		return this.mybatisRepositoryTrack.findMybatisList(statement);
	}

	/**
	 * 查询记录
	 * @author dasuan
	 * @version 1.0
	 * @param statement ,执行的语句名称
	 * @param parameter ,参数Map
	 * @return List<Object>
	 */
	public List<String> findMybatisString(String statement, Map<String, Object> parameter) {
		return this.mybatisRepositoryTrack.findMybatisString(statement, parameter);
	}
	
	/**
	 * 查询记录总数
	 * 
	 * @param querySqlName
	 *            ,执行的语句名称
	 * @param paramsMap
	 *            ,参数Map
	 * @return Long,记录数
	 */
	public Long findIbatisListCount(String querySqlName, Map<String, Object> paramsMap) throws DataAccessException {
		return this.mybatisRepositoryTrack.findIbatisListCount(querySqlName, paramsMap);
	}

	/**
	 * 插入记录
	 * 
	 * @param insertqlName
	 *            ,执行的语句名称
	 * @param paramsMap
	 *            ,参数Map
	 * @return int,影响记录数
	 */
	public Object insertIbatisObject(String insertSqlName, Map<String, Object> paramsMap) throws DataAccessException {
		logger.info("ibatis插入记录[" + new Date() + "]->insertSqlName:" + insertSqlName);
		return this.mybatisRepositoryTrack.insertIbatisObject(insertSqlName, paramsMap);
	}

	/**
	 * 插入记录
	 * 
	 * @param insertqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return int,影响记录数
	 */
	public Object insertIbatisObject(String insertSqlName, Object object)throws DataAccessException {
		logger.info("ibatis插入记录[" + new Date() + "]->insertSqlName:" + insertSqlName);
		return this.mybatisRepositoryTrack.insertIbatisObject(insertSqlName, object);
	}
	
	/**
	 * 批量插入记录
	 * 
	 * @param insertqlName ,执行的语句名称
	 * @param paramsMap ,参数Map
	 * @return int,影响记录数
	 */
	public Object batchIbatisObject(String insertSqlName, List<Map<String, Object>> paramsMapList)throws DataAccessException {
		logger.info("ibatis插入记录[" + new Date() + "]->insertSqlName:" + insertSqlName);
		return this.mybatisRepositoryTrack.batchIbatisObject(insertSqlName,paramsMapList);
	}
	
	
	/**
	 * 更新记录
	 * 
	 * @param updateSqlName
	 *            ,执行的语句名称
	 * @param paramsMap
	 *            ,参数Map
	 * @return int,影响记录数
	 */
	public int updateIbatisObject(String updateSqlName, Map<String, Object> paramsMap) throws DataAccessException {
		int record = this.mybatisRepositoryTrack.updateIbatisObject(updateSqlName, paramsMap);
		logger.info("ibatis更新记录[" + new Date() + "]->updateSqlName:" + updateSqlName + ",共影响记录数(" + record + ")");
		return record;
	}

	
	
	public List<HashMap<String, Object>> procedureMybatisList(String statement,
			Map<String, Object> parameter) {
		logger.info("ibatis 调用存储过程[" + new Date() + "]->procedure:" + statement);
		return this.mybatisRepositoryTrack.procedureMybatisList(statement,parameter);
	}

	public List<HashMap<String, Object>> findMybatisList(String statement,
			Map<String, Object> parameter) {
		return this.mybatisRepositoryTrack.findMybatisList(statement,parameter);
	}

	public List<HashMap<String, Object>> getMybatisList(String statement,
			Map<String, Object> parameter) {
		return  this.mybatisRepositoryTrack.getMybatisList(statement,parameter);
	}
}
